package view;

import entity.LoginUser;
import services.Service;
import services.lmpl.Servicelmpl;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

// 主界面
public class IndexJFrame extends JFrame implements MouseListener, ActionListener {
    private LoginUser loginUser;  // 定义用户对象
    private Service service;
    private int signOrder = 0;  // 订购
    private int signPurchase = 0;  // 采购
    private int signProductManagement = 0; // 产品管理
    private int signReport = 0;  // 报表

    // 获取屏幕大小
    private final static int width = Toolkit.getDefaultToolkit().getScreenSize().width;
    private final static int height = Toolkit.getDefaultToolkit().getScreenSize().height;

    private JPanel topPanel;
    private JPanel topMenu;
    private JPanel topPrompt;
    private JPanel centerPanel;

    private JTabbedPane jTabbedPane;
    private JLabel order, purchase, productManagement, report;

    public IndexJFrame(LoginUser loginUser) throws Exception {
        this.loginUser = loginUser;
        service = new Servicelmpl();

        // 设置tab面板缩进
        UIManager.put("TabbedPane.tabAreaInsets", new javax.swing.plaf.InsetsUIResource(0, 0, 0, 0));

        // 设置页面图标
        try {
            Image imgae = ImageIO.read(new File("image/logo.png"));
            this.setIconImage(imgae);
        } catch (IOException e) {
            e.printStackTrace();
        }

        initBackgroundPanel();

        this.setTitle("销售管理系统");
        this.setSize((int) (width * 0.8f), (int) (height * 0.8f));
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e) {
                int flag = JOptionPane.showConfirmDialog(null, "Sure to close?",
                        "Care!", JOptionPane.YES_NO_OPTION,
                        JOptionPane.INFORMATION_MESSAGE);
                if(JOptionPane.YES_OPTION == flag){
                    try {
                        service.close();
                    } catch (FileNotFoundException ex) {
                        ex.printStackTrace();
                    }
                    System.exit(0);
                }
            }
        });
//        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    // 初始化背景面板
    public void initBackgroundPanel() {
        // 全局组件
        JPanel backgroundPanel = new JPanel(new BorderLayout());
        initTop();
        initCenterPanel();

        backgroundPanel.add(topPanel, "North");
        backgroundPanel.add(centerPanel, "Center");

        this.add(backgroundPanel);
    }

    // 初始化顶部面板
    public void initTop() {
        initTopMenu();
        initTopPrompt();
        topPanel = new JPanel(new BorderLayout());
        topPanel.setPreferredSize(new Dimension(width, 40));

        topPanel.add(topMenu, "West");
        topPanel.add(topPrompt, "East");
    }

    public void initTopMenu() {
        topMenu = new JPanel();
        topMenu.setPreferredSize(new Dimension(600, 40));
        topMenu.setOpaque(false);// 不绘制部分或所有像素，从而允许其底层像素透视出来。

        order = CreateMenuLabel(order, "订购", "order");
        purchase = CreateMenuLabel(purchase, "采购", "purchase");
        productManagement = CreateMenuLabel(productManagement, "产品管理", "productManagement");
        report = CreateMenuLabel(report, "报表", "report");
    }

    public JLabel CreateMenuLabel(JLabel jLabel, String text, String name) {
        jLabel = new JLabel(new ImageIcon("./image/" + name + ".png"));
        jLabel.setText("<html><font color='black'>" + text + "</font>&nbsp;</html>");
        jLabel.addMouseListener(this);
        jLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        jLabel.setName(name);
        topMenu.add(jLabel);
        JLabel line = new JLabel("<html>&nbsp;<font color='#D2D2D2'>|</font>&nbsp;</html>");
        if (!"report".equals(name)) {
            topMenu.add(line);
        }
        return jLabel;
    }

    // 初始化顶部的欢迎面板
    public void initTopPrompt() {
        Icon icon = new ImageIcon("image/male.png");
        JLabel jLabel = new JLabel(icon);
        if (loginUser != null) {
            jLabel.setText("<html><font color='black'>欢迎您，</font><font color='#336699'><b>" + this.loginUser.getName() + "</b></font></html>");
        } else {
            jLabel.setText("<html><font color='black'>欢迎您，</font><font color='#336699'><b>" + "</b></font></html>");
        }
        jLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        topPrompt = new JPanel();
        topPrompt.setPreferredSize(new Dimension(180, 40));
        topPrompt.setOpaque(false);
        topPrompt.add(jLabel);
    }

    public void initCenterPanel() {
        centerPanel = new JPanel(new BorderLayout());
        order.setText("<html><font color='black'>" + "订购" + "</font>&nbsp;</html>");
        creatOrder();
        centerPanel.setOpaque(false);
    }

    //创建订购面板
    public void creatOrder() {
        centerPanel.removeAll();
        // 设置tab标题位置
        jTabbedPane = new JTabbedPane(JTabbedPane.TOP);
        jTabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
        jTabbedPane.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        jTabbedPane.addTab("订单",new OrderJPanel(service).backgroundPanel); // TODO:
        jTabbedPane.addTab("客户信息管理",new CustomerManagerJPanel(service).backgroundPanel); // TODO:
        centerPanel.add(jTabbedPane, "Center");
    }

    //创建采购面板
    public void creatPurchaseTab() {
        centerPanel.removeAll();
        // 设置tab标题位置
        jTabbedPane = new JTabbedPane(JTabbedPane.TOP);
        jTabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
        jTabbedPane.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        //TODO: jTabbedPane.addTab("采购单",new ProductsManagerJPanel().backgroundPanel);
        jTabbedPane.addTab("供应商管理",new SupplierManagerJPanel(service).backgroundPanel); // TODO:
        centerPanel.add(jTabbedPane, "Center");
    }

    // 创建/刷新产品管理面板ProductManagement
    public void createOrRefreshProductManagementTab() {
        centerPanel.removeAll();
        jTabbedPane = new JTabbedPane(JTabbedPane.TOP);
        jTabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
        jTabbedPane.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        jTabbedPane.addTab("产品管理", new ProductsManagerJPanel(service).backgroundPanel); //TODO:

        centerPanel.add(jTabbedPane, "Center");
    }

    // 创建报表面板
    public void creatReportTab() {
        centerPanel.removeAll();
        jTabbedPane = new JTabbedPane(JTabbedPane.TOP);
        jTabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
        jTabbedPane.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        jTabbedPane.addTab("报表1", new Report1JPanel( service,this).backgroundPanel);
        jTabbedPane.addTab("报表2", new Report2JPanel( service,this).backgroundPanel);
        centerPanel.add(jTabbedPane, "Center");
    }

    // 初始化辅助变量
    public void initSign() {
        signOrder = 0;
        signPurchase = 0;
        signProductManagement = 0;
        signReport = 0;
    }

    public void changeTextStyle() {
        if (signOrder == 0) {
            order.setText("<html><font color='black'>" + "订购" + "</font>&nbsp;</html>");
        } else {
            order.setText("<html><font color='#336699' style='font-weight:bold'>" + "订购" + "</font>&nbsp;</html>");
        }
        if (signPurchase == 0) {
            purchase.setText("<html><font color='black'>" + "采购" + "</font>&nbsp;</html>");
        } else {
            purchase.setText("<html><font color='#336699' style='font-weight:bold'>" + "采购" + "</font>&nbsp;</html>");
        }
        if (signProductManagement == 0) {
            productManagement.setText("<html><font color='black'>" + "产品管理" + "</font>&nbsp;</html>");
        } else {
            productManagement.setText("<html><font color='#336699' style='font-weight:bold'>" + "产品管理" + "</font>&nbsp;</html>");
        }
        if (signReport == 0) {
            report.setText("<html><font color='black'>" + "报表" + "</font>&nbsp;</html>");
        } else {
            report.setText("<html><font color='#336699' style='font-weight:bold'>" + "报表" + "</font>&nbsp;</html>");
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {  // 鼠标点击事件
        if (e.getSource() == order) {
            initSign();
            signOrder = 1;
            creatOrder();
            changeTextStyle();
        } else if (e.getSource() == purchase) {
            initSign();
            signPurchase = 1;
            creatPurchaseTab();
            changeTextStyle();
        } else if (e.getSource() == productManagement) {
            initSign();
            signProductManagement = 1;
            createOrRefreshProductManagementTab();
            changeTextStyle();
        } else if (e.getSource() == report) {
            initSign();
            signReport = 1;
            creatReportTab();
            changeTextStyle();
        }
    }
    @Override
    public void mouseEntered(MouseEvent e) {
        if (e.getSource() == order) {
            order.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            order.setText("<html><font color='#336699' style='font-weight:bold'>" + "订购" + "</font>&nbsp;</html>");
        } else if (e.getSource() == purchase) {
            purchase.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            purchase.setText("<html><font color='#336699' style='font-weight:bold'>" + "采购" + "</font>&nbsp;</html>");
        } else if (e.getSource() == productManagement) {
            productManagement.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            productManagement.setText(
                    "<html><font color='#336699' style='font-weight:bold'>" + "产品管理" + "</font>&nbsp;</html>");
        } else if (e.getSource() == report) {
            report.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            report
                    .setText("<html><font color='#336699' style='font-weight:bold'>" + "报表" + "</font>&nbsp;</html>");
        }
    }
    @Override
    public void mouseExited(MouseEvent e) {  // 鼠标划出事件
        if (e.getSource() == order) {
            if (signOrder == 0) {
                order.setText("<html><font color='black'>" + "订购" + "</font>&nbsp;</html>");
            }
        } else if (e.getSource() == purchase) {
            if (signPurchase == 0) {
                purchase.setText("<html><font color='black'>" + "采购" + "</font>&nbsp;</html>");
            }
        } else if (e.getSource() == productManagement) {
            if (signProductManagement == 0) {
                productManagement.setText("<html><font color='black'>" + "产品管理" + "</font>&nbsp;</html>");
            }
        } else if (e.getSource() == report) {
            if (signReport == 0) {
                report.setText("<html><font color='black'>" + "报表" + "</font>&nbsp;</html>");
            }
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }
}
